#include <Arduino.h>
#include "pin_config.h"

#include "WiFi.h"

#include <stdio.h>
#include <stdlib.h>

/* external library */
#include "OneButton.h" // https://github.com/mathertel/OneButton
#include "TFT_eSPI.h"  // https://github.com/Bodmer/TFT_eSPI
#include "lvgl.h"
#include <FastLED.h> // https://github.com/FastLED/FastLED

#include "lvgl_gui.h"
#include "drv_led.h"
#include "drv_tft.h"
#include "drv_sd_card.h"

OneButton button(BTN_PIN, true);
WiFiClient client;
Led sysLed;
SD_Card card;

static uint8_t page_number;

void button_click() {
  page_number = (page_number + 1) % GUI_PAGE_COUNT;
  gui_switch_page(page_number);
}

void setup() {
  Serial.begin(115200);
  Serial.println("Hello T-Dongle-S3");

  drv_tft_init();

  if (card.begin() != 0) {
    uint16_t x = tft.getCursorX();
    uint16_t y = tft.getCursorY();
    while (1) {
      tft.fillRect(x, y, tft.width(), 8, TFT_BLACK);
      tft.setCursor(x, y);
      tft.setTextColor(TFT_RED, TFT_BLACK);
      tft.println("SD card error");
      delay(500);
      tft.fillRect(x, y, tft.width(), 8, TFT_BLACK);
      tft.setCursor(x, y);
      tft.setTextColor(TFT_BLACK, TFT_RED);
      tft.println("SD card error");
      delay(500);
    }
  }
  else {
    tft.println("SD card done");
  }

  tft.println("wait for LVGL");

  delay(1000);

  gui_init();
  gui_switch_page(0);

  button.attachClick(button_click);

  // WiFi.begin(SSID, password);

  // while (WiFi.status() != WL_CONNECTED) {
  //   delay(500);
  // }

}

static bool led_status = true;

void loop() {
  lv_timer_handler();
  button.tick();
  delay(1);
}


